package com.chenglu.android.mine.coupon.fragment;

import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenglu.android.R;
import com.chenglu.android.common.base.BaseMvpFragment;
import com.chenglu.android.common.base.Global;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.SPUtils;
import com.chenglu.android.mine.bean.CouponBean;
import com.chenglu.android.mine.coupon.CouponActivity;
import com.chenglu.android.mine.coupon.adapter.CouponListAdapter;
import com.chenglu.android.mine.coupon.mvp.contract.CouponContract;
import com.chenglu.android.mine.coupon.mvp.presenter.CouponPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * @author: lyj
 * @Created: by Copyright 2023/3/24 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：我的优惠券
 */
public class CouponYSXFragment extends BaseMvpFragment<CouponPresenter> implements CouponContract.View {
    private static int status;
    @BindView(R.id.rv_coupon)
    RecyclerView rvCoupon;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    int current = 1;
    String aaccessToken;
    @BindView(R.id.ll_loading_data)
    LinearLayout llLoadingData;
    @BindView(R.id.ll_loading_no_data)
    LinearLayout llLoadingNoData;
    private CouponListAdapter couponAdapter;
    List<CouponBean.Data.Records> list = new ArrayList<>();
    private static CouponActivity activity;

    public static Fragment getInstance(CouponActivity couponActivity, int i) {
        activity = couponActivity;
        status = i;
        CouponYSXFragment fragment = new CouponYSXFragment();
        return fragment;
    }

    @Override
    protected void initView() {
        mPresenter = new CouponPresenter();
        mPresenter.attachView(this);
        aaccessToken = SPUtils.getString("AaccessToken", "AaccessToken");
        initCommentRv();
        initRefreshLayout();
//        mPresenter.getCouponList(aaccessToken, current, Constant.SIZE, status);
    }

    @Override
    public void onResume() {
        super.onResume();

        list.clear();
        current=1;
        mPresenter.getCouponList(aaccessToken, setMapParams());
    }


    public Map<String,String> setMapParams(){
        Map<String,String> map  = new HashMap<>();
        map.put("current",current+"");
        map.put("size",Constant.SIZE+"");
        map.put("useStatus",String.valueOf(status));

        return map;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_coupon;
    }

    private void initCommentRv() {
        couponAdapter = new CouponListAdapter(mContext, R.layout.item_order_coupon_list, list);
        rvCoupon.setLayoutManager(new LinearLayoutManager(mContext));
        rvCoupon.setAdapter(couponAdapter);
        //显示和隐藏
        couponAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                couponAdapter.setThisTagPosition(position);

                switch (view.getId()) {
                    case R.id.rl_isshow:
                        if (!couponAdapter.getData().get(position).isSelect()) {
                            couponAdapter.getData().get(position).setSelect(true);

                        } else {
                            couponAdapter.getData().get(position).setSelect(false);
                        }

                        break;
                }
                couponAdapter.notifyItemChanged(position);
            }
        });
    }

    //刷新完成
    private void finishRefresh(RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    //刷新
    private void initRefreshLayout() {

        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //id 需要传参过来
                current = 1;
                //需要清空数据再加
                list.clear();
                mPresenter.getCouponList(aaccessToken, setMapParams());


            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                current += 1;
                mPresenter.getCouponList(aaccessToken, setMapParams());

            }
        });
    }
     boolean isAddFooterView  =true;
    @Override
    public void getCouponListSuccess(CouponBean bean) {
        finishRefresh(smartrefreshlayout);
        if (bean.getData().getRecords().size() > 0) {
            llLoadingData.setVisibility(View.VISIBLE);
            llLoadingNoData.setVisibility(View.GONE);
            for (int i = 0; i < bean.getData().getRecords().size(); i++) {
                //添加集合中banner的数据
                list.add(bean.getData().getRecords().get(i));

            }
            if (current * Constant.SIZE > bean.getData().getTotal()) {
                smartrefreshlayout.setEnableLoadMore(false);

                if(isAddFooterView){
                    View inflate = Global.inflate(R.layout.loadmore_addfooter_layout);
                    inflate.findViewById(R.id.tv_no_more).setBackgroundColor(getResources().getColor(R.color.color_f6f6f6));
                    couponAdapter.addFooterView(inflate);
                    isAddFooterView = false;
                }


            } else {
                isAddFooterView = true;
                couponAdapter.removeAllFooterView();
                smartrefreshlayout.setEnableLoadMore(true);

            }
//            stopLoadMoreRefresh(smartrefreshlayout,bean.getData().getTotal(),current,Constant.SIZE);
            couponAdapter.notifyDataSetChanged();

        } else {
            if (list.isEmpty()) {
//                showEmptyView("暂无数据");
                llLoadingData.setVisibility(View.GONE);
                llLoadingNoData.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onError(String errorMsg) {
        isLogin = false;
        SPUtils.setBoolean("ISLOGIN", false);
        showEmptyView("暂无数据");
        activity.showXPopupView(activity, "提示", "登录已过期,请重新登录", "登录");
    }

    @Override
    public void onNetOutTimeError(String tag) {
        super.onNetOutTimeError(tag);
        stopRefresh(smartrefreshlayout);
    }
}
